package cn.wit.summit.chwan.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.ui.activity.MainActivity;
import cn.wit.summit.chwan.ui.base.BaseFragment;

/**
 * Created by Alan on 2018/8/3.
 */

public class MakeMoneyV2Fragment extends BaseFragment {

    @Bind(R.id.makeMoneyTopLeftTv)
    TextView makeMoneyTopLeftTv;
    @Bind(R.id.makeMoneyTopLeftIv)
    ImageView makeMoneyTopLeftIv;
    @Bind(R.id.makeMoneyTopRightTv)
    TextView makeMoneyTopRightTv;
    @Bind(R.id.makeMoneyTopRightIv)
    ImageView makeMoneyTopRightIv;

    private FragmentManager fragmentManager;
    private MakeMoneyRecommendFragment recommendFragment;
    private MakeMoneyRankingIndexFragment rankingIndexFragment;
    private String makemoneyOneTag = "makemoneyone";
    private String makemoneyTwoTag = "makemoneytwo";
    private int selectedPo = 1;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_make_money_v2, container, false);
        ButterKnife.bind(this, view);

        initView(savedInstanceState);
        seleteChangeTop(1);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (((MainActivity)getActivity()).getSelectedPo()==2)
            ((MainActivity)getActivity()).showDialog();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden && ((MainActivity)getActivity()).getSelectedPo()==2)
            ((MainActivity)getActivity()).showDialog();
    }

    public void setSelectIndex(){
        seleteChangeTop(1);
    }

    private void initView(@Nullable Bundle savedInstanceState) {
        makeMoneyTopLeftTv.setText("推荐");
        makeMoneyTopRightTv.setText("排行");

        fragmentManager = getActivity().getSupportFragmentManager();
        if (savedInstanceState != null) {
            recommendFragment = (MakeMoneyRecommendFragment) fragmentManager.findFragmentByTag(makemoneyOneTag);
            rankingIndexFragment = (MakeMoneyRankingIndexFragment) fragmentManager.findFragmentByTag(makemoneyTwoTag);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.makeMoneyTopLeftLl, R.id.makeMoneyTopRightLl})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.makeMoneyTopLeftLl:
                seleteChangeTop(1);
                break;
            case R.id.makeMoneyTopRightLl:
                seleteChangeTop(2);
                break;
        }
    }

    private void seleteChangeTop(int position) {
        if (getActivity()==null) {
            return;
        }
        if (fragmentManager==null) {
            fragmentManager = getActivity().getSupportFragmentManager();
        }
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
        selectedPo = position;

        if (position==1) {
            makeMoneyTopLeftTv.setTextColor(getActivity().getResources().getColor(R.color.color_white));
            makeMoneyTopLeftIv.setVisibility(View.VISIBLE);
            makeMoneyTopRightTv.setTextColor(getActivity().getResources().getColor(R.color.makemoney_top_unselete_color));
            makeMoneyTopRightIv.setVisibility(View.INVISIBLE);

            if (recommendFragment == null) {
                recommendFragment = new MakeMoneyRecommendFragment();
                transaction.add(R.id.makeMoneyMainFl, recommendFragment, makemoneyOneTag);
            } else {
                transaction.show(recommendFragment);
            }
        } else {
            makeMoneyTopLeftTv.setTextColor(getActivity().getResources().getColor(R.color.makemoney_top_unselete_color));
            makeMoneyTopLeftIv.setVisibility(View.INVISIBLE);
            makeMoneyTopRightTv.setTextColor(getActivity().getResources().getColor(R.color.color_white));
            makeMoneyTopRightIv.setVisibility(View.VISIBLE);

            if (rankingIndexFragment == null) {
                rankingIndexFragment = new MakeMoneyRankingIndexFragment();
                transaction.add(R.id.makeMoneyMainFl, rankingIndexFragment, makemoneyTwoTag);
            } else {
                transaction.show(rankingIndexFragment);
            }
        }

        try {
            transaction.commit();
        } catch (Exception e) {
        }
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (recommendFragment != null) {
            transaction.hide(recommendFragment);
        }
        if (rankingIndexFragment != null) {
            transaction.hide(rankingIndexFragment);
        }
    }
}
